/******************************************************************************/
/*!
\file   Window.cpp
\author David Seah Seng Chong
\par    email: ssc1987@gmail.com
\date 26/12/2011
\brief
    Header file
    System Header

      Functions included in file:\n

    Copyright (C) 2011 
    Reproduction or disclosure of this file or its contents without the
    prior written consent is prohibited.
*/
/******************************************************************************/

#include "Window.hpp"

namespace ReekEngine
{
LPCTSTR szClassName = TEXT("Reek Engine");

bool CWindow::Initialize()
{
    
    m_hInstance = GetModuleHandle(NULL);
    WNDCLASS wc = {0};
    wc.style = CS_OWNDC;
	wc.lpfnWndProc = WinProc;
	wc.cbClsExtra = 0;
	wc.cbWndExtra	= 0;
	wc.hInstance = m_hInstance;
	wc.hIcon = LoadIconA(NULL, IDI_WINLOGO);
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
    wc.lpszClassName = szClassName;
    RegisterClass(&wc);

	m_hWnd = CreateWindowEx(
	NULL,
	szClassName,   // name of the window class
	szClassName,   // title of the window
	WS_CAPTION | WS_OVERLAPPED | WS_SYSMENU | WS_VISIBLE,    // window style
	0,    // x-position of the window
	0,    // y-position of the window
	CSYSTEM->GetWindowWidth(),    // width of the window
	CSYSTEM->GetWindowHeight(),    // height of the window
	NULL,    // we have no parent window, NULL
	NULL,    // we aren't using menus, NULL
	m_hInstance,    // application handle
	NULL);    // used with multiple windows, NULL

	if (m_hWnd == NULL)
	{
		MessageBox(NULL, "Window Initialization FAILED!.", "ERROR", MB_OK);
		SendMessage(NULL, WM_QUIT, 0, 0);
		return false;
	}

	ShowWindow(m_hWnd, SW_SHOW);
	return true;
}

void CWindow::Update()
{
	MSG tMSG;

	while (PeekMessage(&tMSG, NULL, 0, 0, PM_REMOVE))
    {
        if (tMSG.message == WM_QUIT)
        {
            CSYSTEM->EndGame();
        }
        else
        {
            TranslateMessage(&tMSG);
            DispatchMessage(&tMSG);
        }
    }

}

void CWindow::Shutdown()
{
	if (m_hWnd)
	{
		if (!DestroyWindow(m_hWnd))
		{
			MessageBox(NULL, "Window Destruction FAILED!.", "ERROR", MB_OK);
			SendMessage(NULL, WM_QUIT, 0, 0);
		}

		m_hWnd = NULL;
	}
    
    if (!UnregisterClass( szClassName, m_hInstance))
    {
        MessageBox(NULL, "Could not unregister the class.", "ERROR", MB_OK);
        SendMessage(NULL, WM_QUIT, 0, 0);
    }

    m_hInstance = NULL;
}

LRESULT CALLBACK CWindow::WinProc(HWND hWin, UINT unMsg, WPARAM tWP, LPARAM tLP)
{

    switch (unMsg)
    {
    case WM_CREATE:
        break;

    case WM_KEYDOWN:
        break;

    case WM_KEYUP:
        break;

    case WM_LBUTTONDOWN:
        break;

    case WM_MOUSEMOVE:
        break;

    case WM_LBUTTONUP:
        break;
        
    case WM_CLOSE:
        PostQuitMessage(0);
        break;

    default:
        return DefWindowProc(hWin, unMsg, tWP, tLP);
    }

    return 0;
}

}
